#!/usr/bin/python

# I like to put the python version on the #! line,
# so that I can have multiple versions installed.

"""

This is a small wxPython app developed to demonstrate how to write
Pythonic wxPython code.

"""

import wx
import greaterLipsum

class DemoPanel(wx.Panel):
    """This Panel hold two simple buttons, but doesn't really do anything."""
    def __init__(self, parent, *args, **kwargs):
        """Create the DemoPanel."""
        wx.Panel.__init__(self, parent, *args, **kwargs)

        self.parent = parent  

        # make the generate button
        generate_lipsum = wx.Button(self, label="Generate Lipsum Paragraphs")
        generate_lipsum.Bind(wx.EVT_BUTTON, self.generate_lipsum )

        # label for paragraph number chooser
        choiceLabel = wx.StaticText(parent=self, pos=(160, 30))
        choiceLabel.SetLabel('Choose number of paragraphs')
        
        # Paragraph number chooser
        self.paragraphs = "1"
        paragraphs = [str(x) for x in range(1,21)]
        self.choice = wx.Choice(self, -1, (100, 50), choices = paragraphs)
        self.choice.Bind(wx.EVT_CHOICE, self.EventChoice, self.choice)      
        #print [x for x in dir(choice) if x.startswith('Set')]

        # Make the copy button
        copy_lipsum = wx.Button(self, label="Copy to Clipboard (Ctrl-C)")
        copy_lipsum.Bind(wx.EVT_BUTTON, self.copy_lipsum )
        
        # Add in the text area for display of content
        self.text = wx.TextCtrl(self, -1, "",
                       size=(500, 500), style=wx.TE_MULTILINE|wx.TE_PROCESS_ENTER)
              
        
        # lay out the page
        Sizer = wx.BoxSizer(wx.VERTICAL)
        Sizer.Add(choiceLabel, 0, wx.ALIGN_CENTER|wx.ALL, 5)        
        Sizer.Add(self.choice, 0, wx.ALIGN_CENTER|wx.ALL, 5)
        Sizer.Add(generate_lipsum, 0, wx.ALIGN_CENTER|wx.ALL, 5)
        Sizer.Add(copy_lipsum, 0, wx.ALIGN_CENTER|wx.ALL, 5)
        Sizer.Add(self.text, 0, wx.ALIGN_CENTER|wx.ALL, 5)        

        self.SetSizerAndFit(Sizer)


    def generate_lipsum(self, event=None):
        """generate lipsum and savve to self.text."""
        book = greaterLipsum.readFile('cthullu.txt')
        lipsum = '\n\n'.join(greaterLipsum.listLines(book, int(self.paragraphs)))
        self.text.SetValue(lipsum)

    def copy_lipsum(self, event=None):
        """ Copy lipsum to system keyboard"""
        if wx.TheClipboard.Open():
            lipsum = wx.TextDataObject(self.text.GetValue())
            wx.TheClipboard.SetData(lipsum)
            wx.TheClipboard.Flush()              
            wx.TheClipboard.Close()
            self.text.SetValue('Lipsum Copied')         
    
    def EventChoice(self,event):
        # Handle the number of paragraphs being chosen
        self.paragraphs = event.GetString()
        
    def _chooser(self,paragraphs):     
        self.paragraphs = str(paragraphs)
            
    def Choose1(self,event):
        self._chooser(1)
    def Choose2(self,event):
        self._chooser(2)
    def Choose3(self,event):
        self._chooser(3)
    def Choose4(self,event):
        self._chooser(4)
    def Choose5(self,event):
        self._chooser(5)
    def Choose6(self,event):
        self._chooser(6)
    def Choose7(self,event):
        self._chooser(7)
    def Choose8(self,event):
        self._chooser(8)
    def Choose9(self,event):
        self._chooser(9)


   


class MainFrame(wx.Frame):
    """Main Frame holding the Panel."""
    def __init__(self, *args, **kwargs):
        """Create the DemoFrame."""
        wx.Frame.__init__(self, *args, **kwargs)
        
        def doBind(item, handler, updateUI=None):
            self.Bind(wx.EVT_MENU, handler, item)
            if updateUI is not None:
                self.Bind(wx.EVT_UPDATE_UI, updateUI, item)
        

        # Add the Widget Panel
        self.Panel = DemoPanel(self)


        # Build the menu bar
        menubar = wx.MenuBar()

        filemenu = wx.Menu()
        editmenu = wx.Menu()  

              
        
        doBind(filemenu.Append(-1, "E&xit\tCtrl+Q", "Quit this program"),
                self.OnQuit )


        doBind(editmenu.Append(-1, "&Generate Lipsum\tCtrl+G", "Geerate lipsum"),
                self.Panel.generate_lipsum )
        doBind(editmenu.Append(-1, "&Copy Lipsum\tCtrl+C", "Copy lipsum"),
                self.Panel.copy_lipsum )
        editmenu.AppendSeparator()                
        doBind(editmenu.Append(-1, "&Choose 1 Paragraph\tCtrl+1", "Choose 1 Paragraph"),
                self.Panel.Choose1 )        
        doBind(editmenu.Append(-1, "&Choose 2 Paragraphs\tCtrl+2", "Choose 2 Paragraphs"),
                self.Panel.Choose2 )        
        doBind(editmenu.Append(-1, "&Choose 3 Paragraphs\tCtrl+3", "Choose 3 Paragraphs"),
                self.Panel.Choose3 )
        doBind(editmenu.Append(-1, "&Choose 4 Paragraphs\tCtrl+4", "Choose 4 Paragraphs"),
                self.Panel.Choose4 )        
        doBind(editmenu.Append(-1, "&Choose 5 Paragraphs\tCtrl+5", "Choose 5 Paragraphs"),
                self.Panel.Choose5 )        
        doBind(editmenu.Append(-1, "&Choose 6 Paragraphs\tCtrl+6", "Choose 6 Paragraphs"),
                self.Panel.Choose6 )
        doBind(editmenu.Append(-1, "&Choose 7 Paragraphs\tCtrl+7", "Choose 7 Paragraphs"),
                self.Panel.Choose7 )        
        doBind(editmenu.Append(-1, "&Choose 8 Paragraphs\tCtrl+8", "Choose 8 Paragraphs"),
                self.Panel.Choose8 )        
        doBind(editmenu.Append(-1, "&Choose 9 Paragraphs\tCtrl+9", "Choose 9 Paragraphs"),
                self.Panel.Choose9 )




        menubar.Append(filemenu, "&File")
        menubar.Append(editmenu, "&Edit")        
        self.SetMenuBar(menubar)


        self.Fit()
    
       

    def OnQuit(self, event=None):
        """Exit application."""
        self.Close()

if __name__ == '__main__':
    app = wx.App()
    frame = MainFrame(None, title="Greater Lipsum")
    frame.Show()
    app.MainLoop()
